<template lang='pug'>
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-10.pr-10(v-if="hasFind")
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="100")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.value"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-20.pr-20.bg-white
        Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable" @on-selection-change="handleOnSelectChange")
          .clearfix.font-16(slot="header")
            .table-title-lefttxt.pull-left
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
              |条
            .pull-right.table-title-btns
              Button.mr-10(v-if="hasAdd" type="info" @click="handleShowCreateModal()") 新建入职模板
          .clearfix.pl-20.pr-20(slot="footer")
            Page(
              :total="tableComone.totalRows"
              @on-change="onChangePage"
              @on-page-size-change="onpageSizeChange"
              show-sizer
              show-total
              placement="top"
              prev-text="上一页"
              next-text="下一页"
              :page-size-opts="[10, 20, 30, 50, 100]"
            )
          template(slot-scope="{ row, index }" slot="action")
            Button.mr-10(v-if="hasUpdate" type="text" size="small" @click="handleShowCreateModal(row)") 编辑
            Button.mr-10(v-if="hasCopy" type="text" size="small" @click="handleShowCopyModal(row)") 复制
            Button.mr-10(v-if="row.enabled && hasDisEnabled" type="text" size="small" @click="isEnabledModal(row)") 禁用
            Button.mr-10(v-if="!row.enabled && hasEnabled" type="text" size="small" @click="isEnabledModal(row)") 启用

    //- 附件上传弹窗
    CreateModal(ref="CreateModal" @query="query")
</template>

<script>
import config from '@/config'
import { SysQuickEntryTemplateApi } from './SysQuickEntryTemplate.api.js'
import { quickInductionThead } from './tableHead.js'
import _cloneDeep from 'clone-deep'
import CreateModal from './modal/createModal.vue'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
const colStyle = 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-9 ivu-col-span-xxl-6'

export default {
  name: 'quickInduction',

  components: {
    CreateModal
  },

  data () {
    return {
      thead: _cloneDeep(quickInductionThead),
      tableData: [],
      selectedId: [], // 勾选的id集合,array格式
      selectedArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1,
        totalRows: 0,
        pageSize: 10
      },
      formData: {
        name: '',
      },
      formItem: [
        { type: 'input', label: '模板名称：', placeholder: '请输入模板名称', prop: 'name', model: 'name', style: colStyle },
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-3 ivu-col-span-xxl-3' }
      ],
      rules: {},
      arrListDown: {
        AuditStatus: []
      }
    }
  },

  mounted () {
    this.query()
  },

  computed: {
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        return item.code
      })
      return arr
    },

    // 查询
    hasFind () {
      return this.actionArr.indexOf('Find') > -1
    },

    // 查看
    hasView () {
      return this.actionArr.indexOf('View') > -1
    },

    // 新增
    hasAdd () {
      return this.actionArr.indexOf('Add') > -1
    },

    // 编辑
    hasUpdate () {
      return this.actionArr.indexOf('Update') > -1
    },

    // 复制
    hasCopy () {
      return this.actionArr.indexOf('Copy') > -1
    },

    // 启用
    hasEnabled () {
      return this.actionArr.indexOf('Enabled') > -1
    },

    // 禁用
    hasDisEnabled () {
      return this.actionArr.indexOf('DisEnabled') > -1
    },
  },

  methods: {
    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    // 分页页码变动
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedArr = value.map((item) => {
        return { id: item.id, name: item.invoiceItem }
      })
      this.selectedId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedArr)
      console.log('列表勾选数组2', this.selectedId)
    },

    // 分页页码变动
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },

    // 获取列表
    query() {
      var params = {
        ...this.formData,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize,
      }
      SysQuickEntryTemplateApi.GetList(params).then(resp => {
        console.log('%c resp: ', 'color:red', resp)
        if (resp.data.success) {
          this.tableComone.totalRows = resp.data.totalRows
          this.tableData = resp.data.data
        }
      })
    },

    // 新建/编辑弹窗
    handleShowCreateModal(row) {
      this.$refs.CreateModal.show()
      this.$refs.CreateModal.getModal(row ? row.id : null)
    },

    // 复制弹窗
    handleShowCopyModal(row) {
      this.$refs.CreateModal.show()
      this.$refs.CreateModal.getCopyModel(row.id)
    },

    // 是否启用/禁用前询问
    isEnabledModal (row) {
      let txt = row.enabled ? '禁用' : '启用'
      this.$Modal.confirm({
        title: `是否确认${txt}该条信息`,
        onOk: () => {
          this.handleEnabledList(row)
        }
      })
    },

    // 启用/禁用
    handleEnabledList (row) {
      const params = {
        ids: row.id,
        enabled: row.enabled ? false : true
      }
      SysQuickEntryTemplateApi.EnabledList(params).then(resp => {
        console.log('%c 启用/禁用 resp: ', 'color:red', resp)
        if (resp.data.success) {
          this.$Message.success(resp.data.message)
          this.query()
        }
      })
    }
  }
}
</script>

<style lang="less">       
</style>
